package cn.w.outdoor.lib.common;

public class CommonMethods {

    public static boolean isStringNotEmpty(String string) {
        return string != null && !"".equals(string.trim());
    }

    /**
     * 按照十六进制打印byte[]数组
     *
     * @param buffer 要打印的byte[]数组
     * @return 按照先后顺序组成的byte[]打印String
     */
    public static String byte2hex(byte[] buffer) {
        return byte2hex(buffer, buffer.length);
    }

    public static String byte2hex(byte[] buffer, int length) {
        if (buffer != null && length > 0) {
            StringBuilder stringBuffer = new StringBuilder();
            int readResult = Math.min(buffer.length, length);
            for (int i = 0; i < readResult; i++) {
                String temp = Integer.toHexString(buffer[i] & 0xFF).toUpperCase();
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                temp = "0x" + temp;
                stringBuffer.append(" ").append(temp);
            }
            return stringBuffer.toString();
        }
        return null;
    }

    /**
     * 按照十六进制打印byte[]数组
     *
     * @param buffer 要打印的byte[]数组
     * @return 按照先后顺序组成的byte[]打印String
     */
    public static String byte2hexNum(byte[] buffer, String splite) {
        if (buffer != null) {
            StringBuilder stringBuffer = new StringBuilder();
            for (int i = 0; i < buffer.length; i++) {
                String temp = Integer.toHexString(buffer[i] & 0xFF).toUpperCase();
                if (temp.length() == 1) {
                    temp = "0" + temp;
                    temp += splite;
                }
                stringBuffer.append(temp);
            }
            return stringBuffer.toString();
        }
        return null;
    }

    public static String byte2hexNum(byte[] buffer) {
        return byte2hexNum(buffer, "");
    }

    /**
     * 获取byte[]的打印序列
     *
     * @param buffer 要打印的Byte数组
     * @param split  数字间的分隔符
     * @return 生成的打印序列
     */
    public static String byte2String(byte[] buffer, String split) {
        StringBuffer stringBuffer = new StringBuffer();
        if (split == null) {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(buffer[i]);
                if (i != buffer.length - 1) {
                    stringBuffer.append(split);
                }
            }
        } else {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(buffer[i]);
                if (i != buffer.length - 1) {
                    stringBuffer.append(" ");
                }
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取无符号byte[]的打印序列
     *
     * @param buffer 要打印的Byte数组
     * @param split  数字间的分隔符
     * @return 生成的打印序列
     */
    public static String unsignedByte2String(byte[] buffer, String split) {
        StringBuffer stringBuffer = new StringBuffer();
        if (split != null) {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(unsignedByteToInt(buffer[i]));
                if (i != buffer.length - 1) {
                    stringBuffer.append(split);
                }
            }
        } else {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(unsignedByteToInt(buffer[i]));
                if (i != buffer.length - 1) {
                    stringBuffer.append(" ");
                }
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取无符号byte[]的打印序列
     *
     * @param buffer 要打印的Byte数组
     * @return 生成的打印序列
     */
    public static String unsignedByte2String(byte[] buffer) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < buffer.length; i++) {
            stringBuffer.append(unsignedByteToInt(buffer[i]));
            if (i != buffer.length - 1) {
                stringBuffer.append(" ");
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取无符号的byte打印，实际是转换成了Int数据
     */
    public static int unsignedByteToInt(byte b) {
        return (int) b & 0xFF;
    }

    public static void intInsertIntoBytes(byte[] bytes, int startIndex, int intValue) {
        bytes[startIndex + 3] = (byte) ((intValue >>> 24) & 0xFF);
        bytes[startIndex + 2] = (byte) ((intValue >>> 16) & 0xFF);
        bytes[startIndex + 1] = (byte) ((intValue >>> 8) & 0xFF);
        bytes[startIndex] = (byte) (intValue & 0xFF);
    }

    public static int bytesToInt(byte[] bytes, int startIndex) {
        int returnValue = 0;
        if (bytes != null) {
            // 由高位到低位
            for (int i = 0; i < 4; i++) {
                int shift = (i) * 8;
                returnValue += (bytes[i + startIndex] & 0x000000FF) << shift;// 往高位游
            }
        }
        return returnValue;
    }

}
